package com.example.gamecenter

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.example.gamecenter.fragments.explore.ExploreFragment
import com.example.gamecenter.fragments.home.HomeFragment
import com.example.gamecenter.fragments.my.MyFragment
import com.google.android.material.button.MaterialButton
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private val context = this;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        tab_group.addOnButtonCheckedListener { group, checkedId, isChecked ->
            val childCount: Int = group.childCount;
            var selectIndex = 0;
            for (index in 0 until childCount){
                val childAt = group.getChildAt(index) as MaterialButton;
                if (childAt.id == checkedId){
                    selectIndex = index;
                    childAt.setTextColor(ContextCompat.getColor(context, R.color.primary));
                    childAt.iconTint = ColorStateList.valueOf(ContextCompat.getColor(context,R.color.primary));
                } else {
                    childAt.setTextColor(Color.BLACK);
                    childAt.iconTint = ColorStateList.valueOf(Color.BLACK);
                }
            }
             switchFragment(selectIndex);
        }
        tab_group.check(R.id.tab1)
    }
    // 首页fragment
    private var homeFragment : HomeFragment?=null;
    // 探索fragment
    private var exploreFragment : ExploreFragment?=null;
    // 我的fragment
    private var myFragment : MyFragment?=null;
    // 切换时中间变量
    private var shownFragment : Fragment?=null;

    // 切换tab
    private fun switchFragment(selectIndex: Int) {
        val fragment: Fragment = when(selectIndex){
            0-> {
                if (homeFragment == null){
                    homeFragment = HomeFragment();
                    // fragment 传参
                    val bundle = Bundle();
                    bundle.putString("tab", "tab1")
                    homeFragment!!.arguments = bundle;
                }
                homeFragment;
            }
            1-> {
                if (exploreFragment == null){
                    exploreFragment = ExploreFragment();
                    // fragment 传参
                    val bundle = Bundle();
                    bundle.putString("tab", "tab1")
                    exploreFragment!!.arguments = bundle;
                }
                exploreFragment;
            }
            2-> {
                if (myFragment == null){
                    myFragment = MyFragment();
                    // fragment 传参
                    val bundle = Bundle();
                    bundle.putString("tab", "tab1")
                    myFragment!!.arguments = bundle;
                }
                myFragment;
            }
            else -> {
                // 不作操作
                throw IllegalArgumentException("Invalid value")
            }
        } ?: return;
        val ft = supportFragmentManager.beginTransaction();
        if (!fragment.isAdded) {
            ft.add(R.id.fragment_main_container, fragment)
        }
        ft.show(fragment);
        // 切换时隐藏其他的
        if (shownFragment != null) {
            ft.hide(shownFragment!!)
        }
        shownFragment = fragment;
        ft.commitAllowingStateLoss();
    }
}